<?php

include('../../include/functions.inc.php');
include('../../include/inputbuilder.inc.php');
if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 's':
            save($db, $user);
            break;
        case 'u':
            update($db);
            break;
        case 'd':
            delete($db);
            break;
        case 'ac': {
                activateCycle($db);
            }
    }
}

function save($db, $user) {
    $begin = date('Y-m-d', strtotime(date(getParam('DATE_BEGIN'))));
    $end = date('Y-m-d', strtotime(date(getParam('DATE_END'))));
    $db->query_insert('cllg_college_cycle', array(
        'CODE' => sqlValue(getParam('CODE'), 'int'),
        'DATE_BEGIN' => sqlValue($begin, 'date'),
        'DATE_END' => sqlValue($end, 'date'),
        'FK_ENTITY' => sqlValue(getParam('FK_ENTITY'), 'int'),
        'CREATED_BY' => sqlValue($user->name, 'text'),
        'CREATED' => sqlValue(getParam('CREATED'), 'date')));
    //echo 'SAVED';
}

function update($db) {
    $begin = date('Y-m-d', strtotime(date(getParam('DATE_BEGIN'))));
    $end = date('Y-m-d', strtotime(date(getParam('DATE_END'))));
    $db->query_update('cllg_college_cycle', array(
        'DATE_BEGIN' => sqlValue($begin, 'date'),
        'DATE_END' => sqlValue($end, 'date'),
        'FK_ENTITY' => sqlValue(getParam('FK_ENTITY'), 'int'))
            , 'CODE=' . sqlValue(getParam('CODE'), 'int'));
    echo 'UPDATED';
}

function delete($db) {
    $db->query_delete('cllg_college_cycle', 'CODE=' . sqlValue(getParam('CODE'), 'int'));
    echo 'DELETED';
}

function activateCycle($db) {
    $db->query_update('cllg_college_cycle', array('STATUS' => sqlValue(0, 'int'),
        'DATE_DISABLE' => sqlValue(date("Y-m-d"), 'date')), 'ACTIVITY=1 AND STATUS=1 AND FK_ENTITY=' . sqlValue(getParam('FK_ENTITY'), 'int'));
    $db->query_update('cllg_college_cycle', array('STATUS' => sqlValue(1, 'int'),
        'ACTIVITY' => sqlValue(1, 'int')), 'CODE=' . sqlValue(getParam('CODE'), 'int') . " AND FK_ENTITY=" . sqlValue(getParam('FK_ENTITY'), 'int'));
}

?>